<%@page import="sun.awt.DisplayChangedListener"%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.io.*, java.sql.*, javax.sql.*"%>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", -1);
    Connection con = com.elookinto.wordlist.Util.getCon();
    String listName = request.getParameter("listName");
    String uid = (String) session.getAttribute("uid");
    String pub = request.getParameter("pub");
    String currentWord = request.getParameter("currentWord");
    if (pub != null && pub.equalsIgnoreCase("true")) {
        uid = "admin";
    }
    if (listName == null) {
        listName = "%";
    }
    int days = 0;
    try {
        days = Integer.parseInt(listName);
    } catch (Exception e) {
    }

    PreparedStatement st = null;
    String note = null;
    st = con.prepareStatement("Select txt from note where listname=? and uid=?");
    st.setString(1, listName);
    st.setString(2, uid);
    ResultSet rs = st.executeQuery();
    if (rs.next()) {
        note = (rs.getString(1));
    }
    if (days == 0) {
        out.println("<B>List:</B> " + listName + "<br/>");
    } else {
        out.println("<B>New Words in Past " + days + " Days</B><br/>");
    }
    if (note != null && note.trim().length() != 0) {
        out.println("<B>Note: </B>" + note + "<hr/>");
    }
    out.println(" <button   title='Press Escape Key to Hide All Openned Dictionaries' onclick='hideAll()'>Hide All Openned Dictionaries</button> <br/>");
    if (days == 0) {
        st = con.prepareStatement("Select distinct word, listname, def  from wordlist where listname=? and uid=?");
        st.setString(1, listName);
        st.setString(2, uid);
    } else {
        st = con.prepareStatement("Select distinct word, def from wordlist where uid=? and insert_time > sysdate() - ? day");
        st.setString(1, uid);
        st.setInt(2, days);
    }

    rs = st.executeQuery();
    while (rs.next()) {
        String word = rs.getString("word");
        String def = rs.getString("def");
        String display;
        if (word.equals(currentWord)) {
            display = "";
        } else {
            display = "display:none";
        }
        //System.out.println(rs.getDate("insert_time"));
        //System.out.println(rs.getString("listname"));
        //System.out.println(rs.getString("uid"));
        //System.out.println(word);
        if (days == 0) {
%>

<button style="color:red" onclick='del("<%= word%>", "<%= listName%>")'> X </button>  
<%}%>
<!--   
<img src="http://onelook.com/img/speaker-icon.gif"    onclick="play1('<%=word%>')" /> -->
<a id='b0<%=word%>' title="onelook" class="button" href='javascript:void(0)' onclick= "wordClick(this,'<%= word%>', 0);"><span>O</span></a>
<a id='b1<%=word%>'  title="WordSmyth Beginner's Dictionary" class="button" href='javascript:void(0)' onclick="wordClick(this,'<%= word%>', 1)"><span>B</span></a>
<a id='b2<%=word%>' title="WordSmyth Children's Dictionary" class="button" href='javascript:void(0)' onclick="wordClick(this,'<%= word%>', 2)"><span>C</span></a>
<a id='b3<%=word%>' title="WordSmyth Advanced Dictionary" class="button" href='javascript:void(0)' onclick="wordClick(this,'<%= word%>', 3)"><span>A</span></a>
<%-- <a  id='b4<%=word%>' title="Google Dictionary" class="button" href='javascript:void(0)' onclick="wordClick(this,'<%= word%>', 4)"><span>G</span></a> --%>
<a id='id<%= word%>' class="contextMenuable" title="Double click to insert the word to upload words area; Right click to add the word to one of your lists" 
   href='javascript:void(0)' onDblClick="insertWordToUpload('<%= word%>',  '<%= listName%>')" style="text-decoration: none;font-family:monospace;" ><%= word%></a> 
<span style="font-family:monospace;">  <% if (def != null) {
        for (int m = 0; m < 20 - word.length(); m++) {
            out.print('-');
        }
    }%>
    <%= def == null ? "" : def%> </span>
<iframe    width=100% height=1000 style='<%= display%>' id="<%= word%>"> </iframe> <br/>

<%}%>
<ul id="myMenu" class="contextMenu">

    <b> Add this word to my list:</b>
    <li class='edit' ><a href='#edit' style="color:brown"> newlist </a></li>
    <%
        out.println("<li class='edit'><a href='#Recent'>Recent</a></li>");
        String myuid = (String) session.getAttribute("uid");
        st = con.prepareStatement("Select distinct listname  from wordlist where uid=? order by listname");
        st.setString(1, myuid);

        rs = st.executeQuery();
        while (rs.next()) {
            String mylistName = rs.getString("listname");
            if (mylistName.equals("Recent")) {
                continue;
            }
            out.println("<li class='edit'><a href='#" + mylistName + "'>" + mylistName + "</a></li>");
        }
    %>


</ul>

<%
    rs.close();
    st.close();
    con.close();
%>

<script>
    transformButton();
    //var currentDic = $
    currentButton = document.getElementById("b"+3+ '<%=currentWord%>');
    wordClick(currentButton, '<%=currentWord%>', 3);
    <%--
    $(function() {
        $( ".draggable" ).draggable({ revert: true});
        $( ".droppable" ).droppable({
            activeClass: "ui-state-hover",
            hoverClass: "ui-state-active",
            drop: function( event, ui ) {
                //alert();
                listName = $(this).text();
                $.post("save.jsp", {listName:$(this).text(),wordList:ui.draggable.text()}, function(data){
                    //alert("Saved " + ui.draggable.text());
                    //$('#message').css('display', 'block'); 
                    $("#message").text("Saved '" + ui.draggable.text() + "' to list " + listName +"!");
                })
                
            }
        });
    });
    --%>
        $(".contextMenuable").contextMenu({
            menu: 'myMenu'
        },
        function(action, el, pos) {
       
            listName = action;
            //alert(listName);
            $.post("save.jsp", {listName:listName,wordList:$(el).text()}, function(data){
                //alert("Saved " + ui.draggable.text());
                //$('#message').css('display', 'block'); 
                $("#message").text("Saved '" + $(el).text() + "' to list " + listName +"!");
            });
        });
    
    <%--
    $('.draggable').qtip(
    {
        id: 'modal', // Since we're only creating one modal, give it an ID so we can style it
        content: {
            text: 'Loading...', // The text to use whilst the AJAX request is loading
            ajax: {
                url: 'test/atext.jsp', // URL to the local file
                type: 'GET', // POST or GET
                data: {}, // Data to pass along with your request
                success: function(data, status) {
                    // Process the data
 
                    // Set the content manually (required!)
                    this.set('content.text', data);
                }
            }
        },
        /*position: {
                        my: 'center', // ...at the center of the viewport
                        at: 'center',
                        target: $(window)
                },*/
        show: {
            event: 'mouseover', // Show it on click...
            solo: true, // ...and hide all other tooltips...
            modal: false // ...and make it modal
        },
        hide: false,
        style: 'ui-tooltip-light ui-tooltip-rounded'
    });
    --%>
    <%-- 
    $("iframe").each(function()
  {
      $(this).ready( function() { alert(2);
     $(document.getElementById($(this).attr('id')).contentWindow.document).keyup(function(event) {
         alert(1);
            if ( event.keyCode == 27 ) hideAll();  
     });
      });
});
    --%>
</script>